Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

[...nextauth].ts 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
  2. import NextAuth from 'next-auth'
  3. import Providers from 'next-auth/providers'
  4. export default function Auth(
  5. req: NextApiRequest,
  6. res: NextApiResponse
  7. ): ReturnType<NextApiHandler> {
  8. return NextAuth(req, res, {
  9. providers: [
  10. Providers.GitHub({
  11. clientId: process.env.GITHUB_ID,
  12. clientSecret: process.env.GITHUB_SECRET,
  13. scope: 'read:user',
  14. }),
  15. ],
  16. callbacks: {
  17. async redirect(url, baseUrl) {
  18. return url.startsWith(baseUrl) ? url : baseUrl
  19. },
  20. async signIn(user, account, profile: any) {
  21. const canLogin = await isSponsoringMe(profile?.login)
  22. if (canLogin) {
  23. return canLogin
  24. } else {
  25. return '/sponsorware'
  26. }
  27. },
  28. },
  29. })
  30. }
  31. const whitelist = ['steveruizok']
  32. async function isSponsoringMe(login: string) {
  33. if (whitelist.includes(login)) return true
  34. const res = await fetch('https://api.github.com/graphql', {
  35. method: 'POST',
  36. headers: {
  37. 'Content-Type': 'application/json',
  38. Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
  39. },
  40. body: JSON.stringify({
  41. query: `
  42. query {
  43. user(login: "steveruizok") {
  44. isSponsoredBy(accountLogin: "${login}")
  45. }
  46. }
  47. `,
  48. }),
  49. }).then((res) => res.json())
  50. return res?.data?.user?.isSponsoredBy
  51. }